package com.burial.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.burial.dto.SaveOrderDTO;
import com.burial.entity.Order;
import com.burial.entity.OrderInfo;
import com.burial.entity.PageBean;
import com.burial.entity.vo.OrderVO;
import com.burial.mapper.OrderMapper;
import com.burial.service.OrderInfoService;
import com.burial.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqgc.entity.User;
import com.cqgc.mapper.UserMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 订单信息表 服务实现类
 * </p>
 *
 * @author burial
 * @since 2024-04-17
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private OrderInfoService orderInfoService;

    // 分页查询
    @Override
    public PageBean pageOrder(Integer page, Integer limit) {
        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<Order>().ne(Order::getState,-1);
        // 查询总数
        int count = this.count(wrapper);
        // 获取数据
        int start = (page - 1) * limit;
        List<Order> orderList = orderMapper.pageOrder(start,limit);
        // 修改
        List<OrderVO> orderVOList = new ArrayList<>();
        for (Order order : orderList) {
            User supplier = userMapper.selectById(order.getSupplierId());
            User purchase = userMapper.selectById(order.getPurchaseId());
            OrderVO orderVO = new OrderVO();
            orderVO.setOid(order.getOid());
            orderVO.setSupplier(supplier.getUsername());
            orderVO.setPurchase(purchase.getUsername());
            orderVO.setState(order.getState());
            orderVOList.add(orderVO);
        }
        // 封装
        PageBean pageBean = new PageBean(count, orderVOList);
        return pageBean;
    }

    @Override
    @Transactional
    public boolean saveOrder(SaveOrderDTO orderinfo) {
        boolean ret;
        // 获取orderinfo中的信息封装成实体
        Integer supplierId = orderinfo.getSupplierId();
        Integer purchaseId = orderinfo.getPurchaseId();
        List<OrderInfo> orderInfo = orderinfo.getOrderInfo();
        Order order = new Order();
        order.setSupplierId(supplierId);
        order.setPurchaseId(purchaseId);
        ret=this.save(order);

        if (!ret){
            return ret;
        }
        for (OrderInfo info : orderInfo) {
            info.setOid(order.getOid());
            ret = orderInfoService.save(info);
            if (!ret){
                return ret;
            }
        }
        return ret;
    }

    // 修改订单状态
    @Override
    public void updateState(Long id, Integer state) {
        System.out.println(id);
        Order order = this.getById(id);
        order.setState(state);
        this.updateById(order);
    }

}
